package cn.iocoder.yudao.module.student.service.collectCompany;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyRespDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.student.controller.app.collectCompany.vo.*;
import cn.iocoder.yudao.module.student.dal.dataobject.collectCompany.CollectCompanyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.student.convert.collectCompany.CollectCompanyConvert;
import cn.iocoder.yudao.module.student.dal.mysql.collectCompany.CollectCompanyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.student.enums.ErrorCodeConstants.*;

/**
 * 收藏企业 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CollectCompanyServiceImpl implements CollectCompanyService {

    @Resource
    private CollectCompanyMapper collectCompanyMapper;

    @Override
    public Long createCollectCompany(AppCollectCompanyCreateReqVO createReqVO) {
        // 插入
        CollectCompanyDO collectCompany = CollectCompanyConvert.INSTANCE.convert(createReqVO);
        collectCompanyMapper.insert(collectCompany);
        // 返回
        return collectCompany.getId();
    }

    @Override
    public void updateCollectCompany(AppCollectCompanyUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateCollectCompanyExists(updateReqVO.getId());
        // 更新
        CollectCompanyDO updateObj = CollectCompanyConvert.INSTANCE.convert(updateReqVO);
        collectCompanyMapper.updateById(updateObj);
    }

    @Override
    public void createOrUpdateCollectCompany(@Valid AppCollectCompanyUpdateReqVO updateReqVO) {
        CollectCompanyDO collectCompanyDO=collectCompanyMapper.selectOne("company_id",updateReqVO.getCompanyId(),"student_id",updateReqVO.getStudentId());
        if(updateReqVO.getStatus().equals(0)){//取消
            if (collectCompanyDO != null) {
                collectCompanyMapper.deleteById(collectCompanyDO);
            }
        }
        if(updateReqVO.getStatus().equals(1)){//收藏
            if (collectCompanyDO == null) {
                CollectCompanyDO insetCollectCompanyDO=new CollectCompanyDO();
                insetCollectCompanyDO.setCompanyId(updateReqVO.getCompanyId());
                insetCollectCompanyDO.setStudentId(updateReqVO.getStudentId());
                insetCollectCompanyDO.setId(null);
                insetCollectCompanyDO.setCity(updateReqVO.getCity());
                insetCollectCompanyDO.setCollectTime(new Date());
                collectCompanyMapper.insert(insetCollectCompanyDO);
            }else{
                collectCompanyMapper.reduction(collectCompanyDO.getId());
            }
        }
    }

    @Override
    public void deleteCollectCompany(Long id) {
        // 校验存在
        this.validateCollectCompanyExists(id);
        // 删除
        collectCompanyMapper.deleteById(id);
    }

    private void validateCollectCompanyExists(Long id) {
        if (collectCompanyMapper.selectById(id) == null) {
            throw exception(COLLECT_COMPANY_NOT_EXISTS);
        }
    }

    @Override
    public CollectCompanyDO getCollectCompany(Long id) {
        return collectCompanyMapper.selectById(id);
    }

    @Override
    public List<CollectCompanyDO> getCollectCompanyList(Collection<Long> ids) {
        return collectCompanyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CollectCompanyDO> getCollectCompanyPage(AppCollectCompanyPageReqVO pageReqVO) {
        return collectCompanyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CollectCompanyDO> getCollectCompanyList(AppCollectCompanyExportReqVO exportReqVO) {
        return collectCompanyMapper.selectList(exportReqVO);
    }

    @Override
    public Boolean isCollect(AppCollectCompanyPageReqVO pageReqVO) {
        CollectCompanyDO collectCompanyDO=collectCompanyMapper.selectOne("company_id",pageReqVO.getCompanyId(),"student_id",pageReqVO.getStudentId());
        if(collectCompanyDO!=null){
            return true;
        }
        return false;
    }

}
